#This code produces Figure 1 and Table 2 in the main paper

if(!dir.exists("figs")){dir.create("figs")}
if(!dir.exists("tabs")){dir.create("tabs")}

rm(list = ls())
library(tidyverse)
library(lubridate)
library(foreign)
library(ggthemes)

tishreen = read.csv("tishreen.csv", stringsAsFactors = F)

tishreen = tishreen %>% 
  as_tibble() %>% 
  mutate(date = ymd(date),
         week = floor_date(date, unit = "week"),
         isr_pct = ofhlisrael/totalofhl) %>% 
  group_by(week) %>% 
  mutate(isr_avg = sum(ofhlisrael)/sum(totalofhl))

#Generate missing data
df = tibble(date = seq(ymd("1987-01-03"), ymd("2002-12-31"), by = "1 day"))
tishreen = left_join(df, tishreen, "date")

#Generate week dummies
tishreen = tishreen %>% 
  mutate(year = year(date),
         week = week(date),
         yr_wk = paste0(year, week))

# plots -------------------------------------------------------------------
tishreen %>% 
  ggplot(aes(x = date, y = isr_pct)) + 
  geom_line(colour = "grey") + 
  geom_line(aes(x = date, y = isr_avg)) + 
  theme_few() + 
  labs(x = "Year", y = "Percent of headlines on Israel") + 
  scale_x_date(date_breaks = "1 year", date_labels = "%y")

ggsave("figs/tishreen_israel.pdf", width = 5.5, height = 4)

# peace talks -------------------------------------------------------------
peace_talks = tibble(start = c(ymd("1989-12-27"), ymd("1992-07-22"), ymd("1994-04-30"), ymd("1995-05-24"), ymd("1995-12-11"), ymd("1999-12-08")),
                     end = c(ymd("1991-10-30"), ymd("1993-09-13"), ymd("1994-12-23"), ymd("1995-11-04"), ymd("1996-02-25"), ymd("2000-01-11")))

duration = c(seq.Date(peace_talks$start[1], peace_talks$end[1], by = "day"),
             seq.Date(peace_talks$start[2], peace_talks$end[2], by = "day"),
             seq.Date(peace_talks$start[3], peace_talks$end[3], by = "day"),
             seq.Date(peace_talks$start[4], peace_talks$end[4], by = "day"),
             seq.Date(peace_talks$start[5], peace_talks$end[5], by = "day"),
             seq.Date(peace_talks$start[6], peace_talks$end[6], by = "day"))

tishreen1 = tishreen %>% mutate(talks = ifelse(date > peace_talks$start[1] & date < peace_talks$end[1], 1,
                                               ifelse(date %in% duration, NA, 0)))
tishreen2 = tishreen %>% mutate(talks = ifelse(date > peace_talks$start[2] & date < peace_talks$end[2], 1,
                                               ifelse(date %in% duration, NA, 0)))
tishreen3 = tishreen %>% mutate(talks = ifelse(date > peace_talks$start[3] & date < peace_talks$end[3], 1,
                                               ifelse(date %in% duration, NA, 0)))
tishreen4 = tishreen %>% mutate(talks = ifelse(date > peace_talks$start[4] & date < peace_talks$end[4], 1,
                                               ifelse(date %in% duration, NA, 0)))
tishreen5 = tishreen %>% mutate(talks = ifelse(date > peace_talks$start[5] & date < peace_talks$end[5], 1,
                                               ifelse(date %in% duration, NA, 0)))
tishreen6 = tishreen %>% mutate(talks = ifelse(date > peace_talks$start[6] & date < peace_talks$end[6], 1,
                                               ifelse(date %in% duration, NA, 0)))

library(estimatr)
library(texreg)
mod1 = lm_robust(isr_pct ~ talks, data = tishreen1)
mod2 = lm_robust(isr_pct ~ talks, data = tishreen2)
mod3 = lm_robust(isr_pct ~ talks, data = tishreen3)
mod4 = lm_robust(isr_pct ~ talks, data = tishreen4)
mod5 = lm_robust(isr_pct ~ talks, data = tishreen5)
mod6 = lm_robust(isr_pct ~ talks, data = tishreen6)


texreg(list(mod1, mod2, mod3, mod4, mod5, mod6), custom.model.names = c("1989/12-1991/10", "1992/07-1993/09", "1994/04-1994/12", 
                                                                        "1995/05-1995/11", "1995/12-1996/02", "1999/12-2000/01"), 
       custom.coef.names = c("Intercept", "Peace Talks"), caption = "Coverage of Israel in Tishreen during peace talks", label = "tab:peace_talks",
       fontsize = "footnotesize", include.ci = F, include.rsquared = F, include.adjrs = F, include.nobs = F, reorder.coef = c(2, 1), 
       include.rmse = F, file = "tabs/peace_talks.tex")
